package watsonGenius.Hacker;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import javax.annotation.Resource;
import javax.jws.HandlerChain;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.soap.SOAPFaultException;
import javax.jws.WebParam;
import org.apache.log4j.Logger;
import org.json.JSONObject;

import watsonGenius.Bean.Contacter;
import watsonGenius.Bean.ExchangeTokenBean;
import watsonGenius.Bean.MailSession;
import watsonGenius.Bean.Phone;





@HandlerChain(file = "handler-chain.xml")
@WebService(name = "DataService", targetNamespace = "http://Hacker.Watson.com/")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, 
		parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
//@HandlerChain(file = "handler-chain.xml")


public class DataServiceImpl implements DataService {

	
	
	
	private final Logger logger = Logger.getLogger(DataServiceImpl.class);
	private enum daypicker{
		Sunday,
		Monday,
		Tuesday,
		Wednesday,
		Thusday,
		Friday,
		Saturday
	}
	
	private ArrayList<Contacter> getAllContacters(MailSession m){
		ArrayList<Contacter> cterlist = new ArrayList<Contacter>();
		for(Contacter c: m.getCcPerson()){
			cterlist.add(c);
		}
		for(Contacter c: m.getToPerson()){
			cterlist.add(c);
		}
		cterlist.add(m.getFromPerson());
		return cterlist;
	}
	
	
    //
	@WebMethod()
	public String getInfo(@WebParam(name = "arg0") String requestString){
		JSONObject jo = new JSONObject();
		ExchangeTokenBean etb = new ExchangeTokenBean();
		MailSession mailEvp = new MailSession();
		Contacter cter = new Contacter();
		ArrayList<Object> MailList = new ArrayList<Object>();
		for(int iterator = 0; iterator<10; iterator++){
			mailEvp.setCcPerson(new Contacter[]{cter.CreateContacter(),cter.CreateContacter(),cter.CreateContacter()});
			mailEvp.setDate("11/16/2013");
			mailEvp.setTime("2013-11-16T"+(13+iterator)+":00:00");
			mailEvp.setDay(daypicker.values()[new Random().nextInt(6)].toString());
			mailEvp.setDescription("This is NO."+new Random().nextInt(7786)+" mail content. Please contact me!");
			mailEvp.setEnd_time("2013-11-16T"+(13+iterator)+":25:00");
			mailEvp.setFromPerson(cter.CreateContacter());
			mailEvp.setId(new Random().nextInt(19919));
			mailEvp.setLevel("mid");
			mailEvp.setPretty_time((1+iterator)+":00pm");
			mailEvp.setProposal_type("Plenary");
			mailEvp.setRoom("Room"+ new Random().nextInt(4352));
			mailEvp.setTitle("Personal affair No."+ new Random().nextInt(332525));
			mailEvp.setToPerson(new Contacter[]{cter.CreateContacter()});
			mailEvp.setUnread(false);
			mailEvp.setUrl("");
			mailEvp.setContacters(getAllContacters(mailEvp));
			MailList.add(mailEvp);
		}
		try {
			if(null!=requestString){
				etb.setExchangeData(MailList);
				etb.setTotal((long) MailList.size());
			}else{
				etb.setMessage("Empty request!");
			}
			etb.setSuccess(true);
			//TODO set success message here
			jo = new JSONObject(etb);
		} catch (Exception ex) {
			etb.setMessage(ex.getMessage());
			etb.setSuccess(false);
		}
		logger.debug(jo.toString());
		return jo.toString();
	}
}